package cn.com.jonpad.demo06_simple_project.controller;

import cn.com.jonpad.demo06_simple_project.bean.ResponseInfo;
import cn.com.jonpad.demo06_simple_project.entity.Emploee;
import cn.com.jonpad.demo06_simple_project.repository.EmploeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
public class EmployeeCtrl {
	EmploeeRepository repository;

	@Autowired
	public EmployeeCtrl(EmploeeRepository repository) {
		this.repository = repository;
	}

	@GetMapping("emps")
	public ResponseInfo list(){
		Iterable<Emploee> all = repository.findAll();
		return ResponseInfo.getEntity(all);
	}

	@PostMapping("emp")
	public ResponseInfo add(@RequestBody Emploee emploee){
		repository.save(emploee);
		return ResponseInfo.getEntity(true);
	}

	@PutMapping("emp")
	public ResponseInfo edit(@RequestBody Emploee emploee){
		repository.saveAndFlush(emploee);
		return ResponseInfo.getEntity(true);
	}

	@DeleteMapping("emp/{id}")
	public ResponseInfo delete(@PathVariable("id") Long id){
		repository.deleteById(id);
		return ResponseInfo.getEntity(true);
	}
	@GetMapping("emp/{id}")
	public ResponseInfo add(@PathVariable("id") Long id){
		Optional<Emploee> emploee = repository.findById(id);
		return ResponseInfo.getEntity(emploee);
	}


}
